package ejemplo;

import java.io.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

public class Index extends HttpServlet {

  private String mensaje;
   
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
     mensaje=(String)request.getAttribute("mensaje");
     ServletContext context = getServletContext();
     InputStream header = context.getResourceAsStream("/header.xhtml");
     response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
     if (header != null) {
       InputStreamReader isr = new InputStreamReader(header);
       BufferedReader reader = new BufferedReader(isr);
       String line ="";
       while ((line = reader.readLine()) != null){
         if(Pattern.matches(".*OPER.*", line))
           out.print(line.replaceFirst("OPER", "Entrada"));
         else out.print(line);
       } //end while
     } //endif header

     out.println("<br/><center>");
     out.println("<table>");
     out.println("<form action=\"login\" method=\"post\">");
     out.println("<tr>");
     out.println("<td><font color=\"#FFFFFF\">Login:</font></td>");
     out.println("<td><input type=\"text\" name=\"login\"/></td>");
     out.println("<td/>");
     out.println("</tr>");
     out.println("<tr>");
     out.println("<td><font color=\"#FFFFFF\">Password:</font></td>");
     out.println("<td><input type=\"password\" name=\"password\"/></td>");
     out.println("<td><input type=\"submit\" value=\"Entrar\"/></td>");
     out.println("</tr>");
     out.println("</form>");
     out.println("</table>");

     if(mensaje != null){
        out.println("<br/>");
        out.println("<font color=\"#FFFFFF\" size=+2><b>");
        out.println(mensaje);
        out.println("</b></font>");
     }
     
     out.println("</center>");
     out.println("</body></html>");
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {  
     processRequest(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
     processRequest(request, response);
  }

  @Override
  public String getServletInfo(){ return "Short description"; }
}